package com.poisonh.unifiedauth.dialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;

import net.karthikraj.shapesimage.ShapesImage;

/**
 * 授权登录Dialog
 * Created by PoisonH on 2016/6/24.
 */
public class AuthDialogFragment extends BaseDialogFragment implements View.OnClickListener
{
    private Button mBtnAgree;
    private Button mBtnRefuse;
    private ShapesImage mIvHead;
    private TextView mTvTxt;
    private String mStrAppId;
    private String mStrUUID;
    private String mStrAppName;
    private String mStrAppImg;
    //private String mStrUserId;

//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
//    {
//        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
//        View view = inflater.inflate(R.layout.layout_dialog_auth, null);
//
//        mStrUserId = PreferencesUtils.getString(getActivity(), "ID");
//        mStrAppId = getArguments().getString("app_id");
//        mStrUUID = getArguments().getString("uuid");
//        mStrAppName = getArguments().getString("app_name");
//        mStrAppImg = getArguments().getString("app_img");
//        return view;
//    }

    @Override
    protected View loadView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // mStrUserId = PreferencesUtils.getString(getActivity(), "ID");
        mStrAppId = getArguments().getString("app_id");
        mStrUUID = getArguments().getString("uuid");
        mStrAppName = getArguments().getString("app_name");
        mStrAppImg = getArguments().getString("app_img");
        return inflater.inflate(R.layout.layout_dialog_auth, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        mBtnAgree = (Button) view.findViewById(R.id.btn_agree);
        mBtnRefuse = (Button) view.findViewById(R.id.btn_refuse);
        mIvHead = (ShapesImage) view.findViewById(R.id.iv_auth_img);
        mTvTxt = (TextView) view.findViewById(R.id.tv_tips_type);

        mBtnRefuse.setOnClickListener(this);
        mBtnAgree.setOnClickListener(this);
        mTvTxt.setText("\"" + mStrAppName + "\"");
        if (!mStrAppImg.equals("null"))
        {
            Glide.with(this).load(ConstantUitls.REQUEDT_IMG_URL + mStrAppImg).into(mIvHead);
        } else
        {
            Glide.with(this).load(R.mipmap.ic_launcher).into(mIvHead);
        }
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.btn_agree:
                answer(0);
                break;
            case R.id.btn_refuse:
                answer(1);
                break;
        }
    }

    /**
     * 根据用户点击发起网络请求
     *
     * @param mark
     */
    private void answer(int mark)
    {
        HttpParams params = new HttpParams();
        String mStr = null;
        if (mStrAppId.equals("A"))
        {
            params.put("userid", mStrUserId);
            params.put("mark", mark + "");
            params.put("uuid", mStrUUID);
            mStr = ConstantUitls.AUTH_MYAPP_URL + ConstantUitls.AUTH_MYAPP;
        } else
        {
            params.put("app_id", mStrAppId);
            params.put("user_id", mStrUserId);
            params.put("mark", mark + "");
            params.put("uuid", mStrUUID);
            mStr = ConstantUitls.REQUEDT_URL + ConstantUitls.CONFIRM;
        }
        MyHttpsUtils.post(mStr, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                dismiss();
            }

            @Override
            public void onFailure(String failReason)
            {
                dismiss();
            }
        });
    }
}
